# encoding: utf-8

module Issue

    def add_issue(user, project, title, desc)
        go_in_issue_page(user, project)
        
        @driver.find_element(:link, "新开Issue").click

        #choose issue level 有bug，待修复
        # level = @driver.find_element(:id, "severity_group")
        # level.find_element(:xpath, "//button[@data-toggle='dropdown']").click
        # level.find_element(:xpath, "//a[@severity='minor']").click

        #choose issue type 有bug，待修复
        # type = @driver.find_element(:css, ".issues-right-label")
        # type.find_element(:xpath, "//button[@data-toggle='dropdown']").click
        # type.find_element(:id, "label_1").click

        @driver.find_element(:id, "issue_title").clear
        @driver.find_element(:id, "issue_title").send_keys title
        @driver.find_element(:id, "textarea").clear
        @driver.find_element(:id, "textarea").send_keys desc
        @driver.find_element(:name, "commit").click
    end

    def update_issue(user, project, title)
        go_in_issue_page(user, project)

        @driver.find_element(:link, "#{title}").click

        # 更新级别
        @driver.find_element(:xpath, "//div[@id='severity_group']/button[@data-toggle='dropdown']").click
        @driver.find_element(:xpath, "//a[@severity='minor']").click
        @driver.find_element(:link, "更新").click

        # 更新状态
        @driver.find_element(:xpath, "//div[@id='state_btn_group']/button[@data-toggle='dropdown']").click
        @driver.find_element(:xpath, "//a[@state='resolved']").click
        @driver.find_element(:link, "更新").click

        # 更新拥有者
        @driver.find_element(:xpath, "//div[@id='towho_btn_group']/button[@data-toggle='dropdown']").click
        @driver.find_element(:xpath, "//a[@href='#assign_dialog']").click
        @driver.find_element(:link, "更新").click

        # 更新标签
        @driver.find_element(:xpath, "//div[@class='js-editable-labels-container issues_label btn-group filter-list']/a[@data-toggle='dropdown']").click
        @driver.find_element(:xpath, "//a[@data-label='Bug']").click
        @driver.find_element(:xpath, "//ul[@class='dropdown-menu-active']/span[@class='close']").click

        @driver.find_element(:id, "edit-issue-button").click
        @driver.find_element(:id, "issue_description").send_keys $basic_config['project']['issue_update_desc']
        @driver.find_element(:id, "preview-issue-button").click
        @driver.find_element(:name, "commit").click
    end

    def go_in_issue_page(user, project)
        @driver.get(@base_url+user+'/'+project+'/issues')
    end
end